
document.addEventListener('DOMContentLoaded', function () {
    const titleInput = document.getElementById('titleInput');
    const contentTextarea = document.getElementById('contentTextarea');
    const coverUpload = document.getElementById('coverUpload');
    const coverUploadPlaceholder = document.getElementById('coverUploadPlaceholder');
    const wordCountSpan = document.getElementById('wordCount');
    const publishButton = document.getElementById('publishButton');
    const addTagButton = document.getElementById('addTagButton');
    const tagsInput = document.getElementById('tags');
    const tagList = document.getElementById('tagList');
    const addSourceButton = document.getElementById('addSourceButton');
    const sourceInput = document.getElementById('source');
    const cover_upload = document.getElementsByClassName('cover-upload')[0];
    const deleteButton=document.getElementById('deleteButton')
    // 字数统计
    contentTextarea.addEventListener('input', function () {
        const textLength = contentTextarea.value.length;
        wordCountSpan.textContent = textLength;
    });

    coverUploadPlaceholder.addEventListener('click', function () {
        coverUpload.click();
    });
    // 封面图片上传预览
    cover_upload.addEventListener('change', function (e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function (event) {
                coverUploadPlaceholder.style.backgroundImage = `url(${event.target.result})`;
                coverUploadPlaceholder.style.backgroundSize = 'cover';
                coverUploadPlaceholder.style.backgroundPosition = 'center';
                coverUploadPlaceholder.textContent = '';
                deleteButton.style.display='block'
            };
            reader.readAsDataURL(file);
        }
    });
    deleteButton.addEventListener('click', function () {
        coverUpload.value = ''; // 清空文件选择框
        coverUploadPlaceholder.style.backgroundImage = ''; // 清除背景图片
        coverUploadPlaceholder.textContent = '+ 添加文章封面'; // 恢复提示文本
        deleteButton.style.display = 'none'; // 隐藏删除按钮
    });
    // 添加文章话题
    // addTagButton.addEventListener('click', function () {
    //     const tagText = tagsInput.value.trim();
    //     if (tagText) {
    //         const li = document.createElement('li');
    //         li.textContent = tagText;
    //         tagList.appendChild(li);
    //         tagsInput.value = '';
    //     }
    // });

    // // 添加内容来源
    // addSourceButton.addEventListener('click', function () {
    //     const sourceText = sourceInput.value.trim();
    //     if (sourceText) {
    //         // 这里可以添加将来源信息进行更复杂处理的逻辑，比如存储到数组等
    //         alert('内容来源已添加: ' + sourceText);
    //         sourceInput.value = '';
    //     }
    // });

//     // 模拟发布功能，实际需对接后端接口

//     publishButton.addEventListener('click', function () {
//         const title = titleInput.value;
//         const content = contentTextarea.value;
//         if (title.trim() === '' || content.trim() === '') {
//             alert('标题和正文不能为空');
//             return;
//         }
//         // 这里可以添加对其他发布设置选项的校验逻辑
//         alert('文章发布成功（模拟）');
//     });
});